<?php
	class AsyncController extends BaseController {
		public function shoutIt() {
			if(Auth::check()) {
				if(User::find(Auth::user()->id)->canshout == 1) {
					$rules = array(
						'message' => 'required|between:1,300',
					);

					$validator = Validator::make(Input::all(), $rules);

					if (!$validator->fails())
					{
						$shout = new Shout;
						$shout->message = htmlentities(Input::get("message"));
						$shout->name = Auth::user()->username;
						$shout->save();
					}
				}
			}
		}

		public function shoutList() {
			$date = date("Y-m-d H:i:s", mktime(0,0,0,date("m"),date("d") - 15,date("Y")));

			$shout = Shout::date($date)->count();
			$offset = ($shout - 50) > 0 ? ($shout - 50) : 0;

			return View::make("async.shoutboxcontent")->with(array(
				"shouts" => Shout::date($date)->take(50)->skip($offset)->orderBy('date_time')->get()
			));
		}

		public function getComments() {
			$rules = array(
				'id' => 'required|numeric|exists:news'
			);

			$validator = Validator::make(Input::all(), $rules);

			if (!$validator->fails())
			{
				$comments = Commentaires::news(Input::get('id'))->orderBy('date')->get();
				return View::make('async.news-comment', compact('comments'));
			}
		}

		public function postComment() {
			if(Auth::check()) {
				$rules = array(
					'id' => 'required|numeric|exists:news',
					'message' => 'required|between:1,500'
				);

				$validator = Validator::make(Input::all(), $rules);

				if (!$validator->fails())
				{
					$comment = new Commentaires;
					$comment->user_id = Auth::user()->id;
					$comment->news_id = Input::get('id');
					$comment->commentaire = htmlentities(Input::get('message'));
					$comment->save();
				}
			}
		}
	}